﻿using System.Net.Http.Headers;
using Microsoft.AspNetCore.Http;
using MiDu.Common;

namespace Competition.Infrastructure
{
    public class UploadHelper
    {
        public static string UploadImage(IFormFile file)
        {
            if (file != null)
            {
                string imgName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                var ext = System.IO.Path.GetExtension(imgName);  //图片后缀
                imgName = SysHelper.RandomFileName() + ext;
                string imgPath = System.IO.Path.Combine(System.Environment.CurrentDirectory, "wwwroot", "images", imgName);
                FileHelper.StreamToFile(file.OpenReadStream(), imgPath);
                return "/images/" + imgName;
            }
            return "";
        }
        public static string UploadDownload(IFormFile file)
        {
            if (file != null)
            {
                string imgName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                //var ext = System.IO.Path.GetExtension(imgName);  //图片后缀
                //imgName = SysHelper.RandomFileName() + ext;
                string imgPath = System.IO.Path.Combine(System.Environment.CurrentDirectory, "wwwroot", "downloads", imgName);
                FileHelper.StreamToFile(file.OpenReadStream(), imgPath);
                return "/downloads/" + imgName;
            }
            return "";
        }
    }
}